Understanding Inline, Inline-Block, and Block Elements in the Box Model
In CSS, elements can be displayed as inline, inline-block, or block. Each type behaves differently in how it participates in the box model—particularly regarding width, height, margins, and padding.
Block elements start on a new line and take up the full available width. Their width, height, margin, and padding all affect layout.
Inline elements flow within text and only take up as much width as their content. Vertical margins and heights are ignored, while horizontal margins and padding apply.
Inline-block elements behave like inline elements (don’t break lines) but allow setting width, height, margins, and padding like block elements.
In this example, the block element occupies its own line and full width, the inline element sits within text flow without respecting height, and the inline-block element aligns inline but maintains its own box dimensions.
Block: respects width, height, all margins, and padding.
Inline: ignores width and height; vertical margins have no effect.
Inline-block: respects width, height, margins, and padding, but flows inline with text.